﻿//---------------------------------------------   Framework   -------------------------------------
Ue = {};
Ue.Framework = {};
///转换
Ue.Framework.Convert = {
    ToInt32: function (strNum) {
        var label = parseInt(strNum);
        if (isNaN(label))
            label = 0;
        return label;
    },

    ToBoolean: function (strBool) {
        if (strBool.toString().toLowerCase() == "true" || strBool.toString().toLowerCase() == "1")
            return true;
        else
            return false;
    }
};

//对像类型判断
Ue.Framework.Is = function (obj, type) {
    var toString = Object.prototype.toString, undefined;
    return (type === "Null" && obj === null) ||
          (type === "Undefined" && obj === undefined) ||
          toString.call(obj).slice(8, -1) === type;
};

//深拷贝
Ue.Framework.DeepCopy = function (source) {
    var result = {};
    for (var key in source) {
        var copy = source[key];
        if (result === copy) continue; //如window.window === window，会陷入死循环，需要处理一下
        if (Ue.Framework.Is(copy, "Object")) {
            result[key] = arguments.callee(result[key] || {}, copy);
        } else if (Ue.Framework.Is(copy, "Array")) {
            result[key] = arguments.callee(result[key] || [], copy);
        } else {
            result[key] = copy;
        }
    }
    return result;
};


//----------------------------------------------  JQuery Plugins  --------------------------------------
jQuery.fn.extend({
    //根据浏览器窗口大小变化自动调整对象height与width
    AutoResize: function (settingOptions) {
        var self = this;
        this.settings = {
            reduceWidth: 0,
            reduceHeight: 0,
            orientationMode: "all", //all、height、width 
            referToExpr: window //参照对象jquery expr
        }

        $.extend(true, this.settings, settingOptions || {});

        this.referToControl = $(this.settings.referToExpr);
        if (this.referToControl.length == 0 || this.length == 0) {
            return;
        }

        $(window).resize(function () {

            var width = self.referToControl.width() - self.settings.reduceWidth - self.offsetAllWidth() + self.width();
            var height = self.referToControl.height() - self.settings.reduceHeight - self.offsetAllHeight() + self.height();

            self.each(function () {
                if (this.length == 0)
                    return;

                if (self.settings.orientationMode == 'all' || self.settings.orientationMode == 'width') {
                    if ($(this).css("overflow-y") == "scroll") {
                        width = width - 20;
                    }

                    $(this).width(width);
                }

                if (self.settings.orientationMode == 'all' || self.settings.orientationMode == 'height') {
                    if ($(this).css("overflow-x") == "scroll") {
                        height = height - 20;
                    }

                    $(this).height(height);
                }
            });
        });

        $(window).resize();
    },

    //对象高度 + 边框 + 内补白 + 外补白
    offsetAllHeight: function () {
        var offsetHeight = this[0].offsetHeight;
        if (!offsetHeight) {
            offsetHeight = this.height();
        }

        return offsetHeight + Ue.Framework.Convert.ToInt32(this.css("margin-top")) + Ue.Framework.Convert.ToInt32(this.css("margin-bottom"));
    },
    offsetAllWidth: function () {
        return this[0].offsetWidth + Ue.Framework.Convert.ToInt32(this.css("margin-left")) + Ue.Framework.Convert.ToInt32(this.css("margin-right"));
    }
});

jQuery.fn.extend({
    InputTable: function (className) {
        this.each(function () {
            var cells = parseInt($(this).attr("cells"))
            if (!cells) cells = 2;
            $(this).find(" > tbody > tr > td:not([isTitle=false],[class=btnConfirm])").each(function (i) {
                if (i % cells == 0) $(this).addClass("title");
            });

            $(this).find("td.btnConfirm").attr("colspan", $(this).find("tr:first > td").length);
        });

        if (!className) {
            className = "inputTable";
        }

        this.addClass("inputTable");
    },
    uploadifyByImage: function (settingOptions) {
        var self = this;

        this.settings = {
            targetTextBoxExpr: "#uploadifyTextShow",
            targetImageExpr: "#uploadifyImageShow"
        };

        $.extend(true, this.settings, settingOptions || {});

        if (!$(self.settings.targetImageExpr).attr("src")) {
            $(self.settings.targetImageExpr).hide();
        }

        self.uploadify({
            swf: "/Support/javascript/uploadify/uploadify.swf",
            uploader: "/Support/javascript/uploadify/UploadHandler.aspx",
            buttonText:"上传文件",
            multi: false,
            fileTypeExts: "*.bmp;*.jpg;*.gif;*.jpeg",
            fileTypeDesc: "只允许上传 .bmp,.jpg,.gif,.jpeg",
            width: 60,
            height: 20,
            removeTimeout: 0,
            fileSizeLimit:"200KB",
            onUploadSuccess: function (file, data, response) {
                $(self.settings.targetTextBoxExpr).val(data);
                $(self.settings.targetImageExpr).show().attr("src", data);
            },
            onUploadError: function (file, errorCode, errorMsg, errorString) {
                alert("文件上传错误，请重试。");
            }
        });
    }
});